import { Client } from "./mod.ts";
import { User } from "./user.ts";

/**
 * Release represents a release API object.
 */
export interface Release {
  id: number;
  tag_name: string;
  target_commitish: string;
  name: string;
  body: string;
  draft: boolean;
  prerelease: boolean;
  author: User;
  created_at: string;
}

/**
 * Options for creating a release
 */
export interface CreateReleaseOption {
  tag_name: string;
  target_commitish?: string;
  name?: string;
  body?: string;
  draft?: boolean;
  prerelease?: boolean;
}

/**
 * Options for editing a release
 */
export interface EditReleaseOption {
  tag_name?: string;
  target_commitish?: string;
  name?: string;
  body?: string;
  draft?: boolean;
  prerelease?: boolean;
}

/**
 * Release API endpoints
 */
export class ReleaseAPI {
  constructor(private client: Client) {}

  /**
   * List releases of a repository
   */
  async listReleases(owner: string, repo: string): Promise<Release[]> {
    return await this.client.doRequest<Release[]>(
      "GET", 
      `/repos/${owner}/${repo}/releases`
    );
  }

  /**
   * Get a release by ID
   */
  async getRelease(owner: string, repo: string, id: number): Promise<Release> {
    return await this.client.doRequest<Release>(
      "GET", 
      `/repos/${owner}/${repo}/releases/${id}`
    );
  }

  /**
   * Create a release
   */
  async createRelease(owner: string, repo: string, options: CreateReleaseOption): Promise<Release> {
    return await this.client.doRequest<Release>(
      "POST", 
      `/repos/${owner}/${repo}/releases`, 
      { "Content-Type": "application/json" },
      options
    );
  }

  /**
   * Edit a release
   */
  async editRelease(owner: string, repo: string, id: number, options: EditReleaseOption): Promise<Release> {
    return await this.client.doRequest<Release>(
      "PATCH", 
      `/repos/${owner}/${repo}/releases/${id}`, 
      { "Content-Type": "application/json" },
      options
    );
  }

  /**
   * Delete a release
   */
  async deleteRelease(owner: string, repo: string, id: number): Promise<void> {
    await this.client.doRequest<void>(
      "DELETE", 
      `/repos/${owner}/${repo}/releases/${id}`
    );
  }
} 